package com.lc.zpyh.ui.activity.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.DelAddersslApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.SelectAddressServiceeApi;
import com.lc.zpyh.http.response.SelectAddressServiceeBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.MyDecoration;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.widget.UIHelper;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MineAddressManagementActivity extends AppActivity {
    @BindView(R.id.rv_address_lst)
    SwipeRecyclerView rvAddressLst;
    List<SelectAddressServiceeBean.ListBean> dataBeanList = new ArrayList<>();
    private BaseQuickAdapter<SelectAddressServiceeBean.ListBean, BaseViewHolder> addressadapter;
    private boolean isConfirm;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_address;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        isConfirm = getIntent().getBooleanExtra("isConfirm", false);

    }

    @Override
    protected void onStart() {
        super.onStart();
        picadatper();
    }

    private void picadatper() {
        EasyHttp.post(this)
                .api(new SelectAddressServiceeApi().setSchoolid(SPUtil.get(this, IntentKey.ADDRESSID,"")).setUserid(SPUtil.get(this, IntentKey.USERID,""))).request(new HttpCallback<SelectAddressServiceeBean>(this) {

            @Override
            public void onSucceed(SelectAddressServiceeBean data) {
                if (data.getCode()==0) {
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getList());
                    rvAddressLst.setLayoutManager(new LinearLayoutManager(getContext()));
                    if (addressadapter==null) {
                        addressadapter = new BaseQuickAdapter<SelectAddressServiceeBean.ListBean,
                                BaseViewHolder>(R.layout.item_mineaddressmanagement, dataBeanList) {

                            @Override
                            protected void convert(BaseViewHolder childViewHolder, SelectAddressServiceeBean.ListBean bean) {

                                childViewHolder.setText(R.id.tv_name, bean.getName());
                                childViewHolder.setText(R.id.tv_phone, bean.getPhone());
                                childViewHolder.setText(R.id.tv_address, bean.getCabinetryName());
                                childViewHolder.setGone(R.id.tv_moren, bean.getPatientia() == 0);
                                childViewHolder.getView(R.id.rl_address).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (isConfirm) {
                                            Intent intent = getIntent();
                                            Bundle bundle = new Bundle();
                                            bundle.putSerializable("addressBean", bean);
                                            intent.putExtras(bundle);
                                            setResult(888, intent);
                                            finish();
                                        }
                                    }
                                });
                                childViewHolder.getView(R.id.img_edit).setOnClickListener(view -> {

                                    //编辑地址

                                    startActivity(new Intent(MineAddressManagementActivity.this,AddAddressActivity.class).putExtra("MineAddressBean",bean).putExtra("isUpdate",true));
                                });

                            }

                        };
                        // 创建菜单：
                        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
                            @Override
                            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {

                                SwipeMenuItem deleteItem = new SwipeMenuItem(MineAddressManagementActivity.this); // 各种文字和图标属性设置。
//                deleteItem.setImage(R.mipmap.icon_collent_delete);
                                deleteItem.setText("删除");
                                deleteItem.setTextColor(Color.WHITE);
                                deleteItem.setTextSize(14);
                                deleteItem.setBackgroundColor(Color.parseColor("#FB4747"));

                                deleteItem.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
                                deleteItem.setWidth(300);
                                rightMenu.addMenuItem(deleteItem); // 在Item右侧添加一个菜单。
                                // 注意：哪边不想要菜单，那么不要添加即可。

                            }
                        };

                        // 设置监听器。
                        rvAddressLst.setSwipeMenuCreator(mSwipeMenuCreator);

                        if (rvAddressLst.getItemDecorationCount() == 0) {
                            rvAddressLst.addItemDecoration(new MyDecoration(MineAddressManagementActivity.this,
                                    MyDecoration.VERTICAL_LIST, R.color.colorf5f5f5, 1));
                        }

                        OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
                            @Override
                            public void onItemClick(SwipeMenuBridge menuBridge, int position) {
                                delAdderss(dataBeanList.get(position).getTheGoodsId() + "");
                                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                                menuBridge.closeMenu();
                                // 左侧还是右侧菜单：
                                int direction = menuBridge.getDirection();
                                // 菜单在Item中的Position：
                                int menuPosition = menuBridge.getPosition();

//
                            }
                        };
                        View emptyview = View.inflate(MineAddressManagementActivity.this, R.layout.empty_mine_address_adapter, null);

                        addressadapter.setEmptyView(emptyview);
                        rvAddressLst.setOnItemMenuClickListener(mItemMenuClickListener);

                        rvAddressLst.setAdapter(addressadapter);
                    }else {
                        addressadapter.notifyDataSetChanged();
                    }
                }


            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });

    }

    private void delAdderss(String theGoodsId) {
        EasyHttp.post(this)
                .api(new DelAddersslApi().setAddressid(theGoodsId)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
//
                if (data.getCode() == 0) {
                    toast(data.getList());
                    picadatper();
                }


            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @OnClick(R.id.rl_add_address)
    public void onClick() {
        UIHelper.startActivity(this,AddAddressActivity.class);
    }
}
